package com.qf.shopping.service.activityService.impl;

import com.qf.shopping.mapper.IAdminMangerMapper;
import com.qf.shopping.mapper.activity.ActivityCategoryMapper;
import com.qf.shopping.mapper.activity.ActivityChannelMapper;
import com.qf.shopping.mapper.activity.ActivityMapper;
import com.qf.shopping.pojo.System.User;
import com.qf.shopping.pojo.activity.Activity;
import com.qf.shopping.pojo.activity.ActivityCategory;
import com.qf.shopping.pojo.activity.ActivityChannel;
import com.qf.shopping.service.AdminMangerService;
import com.qf.shopping.service.activityService.IAcitvityService;
import com.qf.shopping.ustils.TimeUtil;
import com.qf.shopping.ustils.UserUtils;
import com.qf.shopping.vo.activity.AcitvityVO;
import com.qf.shopping.vo.activity.constom.ActivityConstom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Mawentao
 * @Date: 2019/4/18 0018 19:42
 * @Description: 活动的实现
 */
@Service
@Transactional
public class AcitvityServiceImpl implements IAcitvityService {

    @Autowired
    private ActivityMapper activityMapper = null;

    @Autowired
    private ActivityCategoryMapper activityCategoryMapper = null;

    @Autowired
    private ActivityChannelMapper activityChannelMapper = null;

    @Autowired
    private IAdminMangerMapper adminMangerMapper = null;

    /**
     * 活动
     * @return
     */
    @Override
    public List<AcitvityVO> getActivityData(AcitvityVO acitvityVO) {
        List<AcitvityVO> activityAll = activityMapper.findActivityLike(null);
        for (AcitvityVO ac : activityAll){
            ActivityConstom activityConstom = ac.getActivityConstom();
            activityConstom.descStates();
        }
        return activityAll;
    }

    @Override
    public List<AcitvityVO> getActivityDataLike(AcitvityVO acitvityVO) {

        List<AcitvityVO> activityLike = activityMapper.findActivityLike(acitvityVO);

        for (AcitvityVO ac : activityLike){
            ActivityConstom activityConstom = ac.getActivityConstom();
            activityConstom.descStates();
        }
        return activityLike;
    }

    @Override
    public Activity getActivityDate(Long id) {
        Activity activityById = activityMapper.findActivityById(id);
        return activityById;
    }


    @Override
    public void addActivity(AcitvityVO acitvityVO, HttpSession session) {
        ActivityConstom activityConstom = acitvityVO.getActivityConstom();
        User user = UserUtils.getUser(session);

        int id = user.getId();
        activityConstom.setCreateUid(id);

        acitvityVO.setActivityConstom(activityConstom);
        activityMapper.insert(acitvityVO);


    }

    @Override
    public void updateActivity(AcitvityVO acitvityVO) {
        Activity activity = activityMapper.findActivityById(acitvityVO.getActivityConstom().getId());

        ActivityConstom activityConstom = acitvityVO.getActivityConstom();

        activity.setTitle(activityConstom.getTitle());
        activity.setDescription(activityConstom.getDescription());
        activity.setCoverPic(activityConstom.getCoverPic());
        activity.setBeginTime(activityConstom.getBeginTime());
        activity.setEndTime(activityConstom.getEndTime());
        activity.setLink(activityConstom.getLink());
        activity.setSeoKeywords(activityConstom.getSeoKeywords());
        activity.setSeoDescription(activityConstom.getSeoDescription());
        //获取活动类型创建者
//        String name = activityConstom.getName();


        activityMapper.updateActivity(activity);
    }

    @Override
    public void publishActivity(Long id,Integer publishState) {
        Map<String ,Object> map = new HashMap<>();
        map.put("id",id);
        map.put("publishState",publishState);
        activityMapper.publishOrNotActivity(map);
    }

    @Override
    public void activityHomeTop(Long id,Integer homeTop) {
        Map<String ,Object> map = new HashMap<>();
        map.put("id",id);
        map.put("homeTop",homeTop);
        activityMapper.gromOrNotActivityHomeTop(map);
    }

    @Override
    public void deleteActivity(Long id) {
        activityMapper.deleteActivity(id);
    }

    @Override
    public List<AcitvityVO> getActivityDataLikeByCategoryId(Long id) {
        AcitvityVO acitvityVO = new AcitvityVO();
        ActivityConstom activityConstom = acitvityVO.getActivityConstom();
        activityConstom.setCategoryId(id);
        acitvityVO.setActivityConstom(activityConstom);
        List<AcitvityVO> activityLike = activityMapper.findActivityLike(acitvityVO);
        return activityLike;
    }

    /**
     * 活动类型
     *
     */
    @Override
    public void deleteActivityCategory(Long id) {
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        map.put("isDeleted",1);
        activityCategoryMapper.deleteActivityCategory(map);
    }

    @Override
    public List<ActivityCategory> findActivityCategory(Long id,Integer isDelete) {
        ActivityCategory activityCategory = new ActivityCategory();
        activityCategory.setId(id);
        activityCategory.setIsDeleted(0);
        List<ActivityCategory> activityCategorys = activityCategoryMapper.findActivityCategory(activityCategory);
        return activityCategorys;
    }

    @Override
    public ActivityCategory getActivityCategory(Long id) {
        ActivityCategory activityCategory = activityCategoryMapper.findActivityCategoryById(id);
        return activityCategory;
    }

    @Override
    public List<ActivityCategory> findActivityCategoryLike(String name) {
        ActivityCategory activityCategory = new ActivityCategory();
        activityCategory.setName(name);
        List<ActivityCategory> activityCategoryLike = activityCategoryMapper.findActivityCategoryLike(activityCategory);
        return activityCategoryLike;
    }

    @Override
    public void updateActivityCategory(ActivityCategory activityCategory) {
        ActivityCategory activityCategoryById = activityCategoryMapper.findActivityCategoryById(activityCategory.getId());

        String name = activityCategory.getName();
        String useCount = activityCategory.getUseCount();
        Long createTime = activityCategory.getCreateTime();

        activityCategoryById.setName(name);
        activityCategoryById.setUseCount(useCount);
        activityCategoryById.setCreateTime(createTime);

        //创建时间
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String formatTime= format.format(date);
        Long time = Long.valueOf(formatTime);

        activityCategoryById.setLastUpdateTime(time);

        activityCategoryMapper.updateActivityCategory(activityCategoryById);

    }

    @Override
    public void insertActivityCategory(ActivityCategory activityCategory) {
        //创建时间
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String formatTime= format.format(date);
        Long time = Long.valueOf(formatTime);

        activityCategory.setLastUpdateTime(0L);
        activityCategory.setIsDeleted(0);
        activityCategory.setCreateTime(time);
        activityCategoryMapper.insert(activityCategory);
    }


    /**
     * 活动渠道
     */
    @Override
    public void deleteActivityChannel(Long id) {
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        map.put("isDeleted",1);
        activityChannelMapper.deleteActivityChannel(map);
    }

    @Override
    public List<ActivityChannel> findActivityChannel(Long id,Integer isDeleted) {
        ActivityChannel activityChannel = new ActivityChannel();
        activityChannel.setId(id);
        activityChannel.setIsDeleted(0);
        List<ActivityChannel> activityChannels = activityChannelMapper.findActivityChannel(activityChannel);
        return activityChannels;
    }



    @Override
    public List<ActivityChannel> findActivityChannelLike(String name) {
        ActivityChannel activityChannel = new ActivityChannel();
        activityChannel.setName(name);
        List<ActivityChannel> activityChannelLike = activityChannelMapper.findActivityChannelLike(activityChannel);
        return activityChannelLike;
    }

    @Override
    public void updateActivityChannel(ActivityChannel activityChannel) {

        ActivityChannel activityChannelById = activityChannelMapper.findActivityChannelById(activityChannel.getId());
        String name = activityChannel.getName();
        String useCount = activityChannel.getUseCount();
        Long createTime = activityChannel.getCreateTime();

        activityChannelById.setName(name);
        activityChannelById.setUseCount(useCount);
        activityChannelById.setCreateTime(createTime);

        Long time = TimeUtil.getTime();
        activityChannelById.setLastUpdateTime(time);
        activityChannelMapper.updateActivityChannel(activityChannel);
    }

    @Override
    public void insertActivityChannel(ActivityChannel activityChannel) {

        activityChannel.setLastUpdateTime(0L);
        activityChannel.setIsDeleted(0);
        Long time = TimeUtil.getTime();
        activityChannel.setCreateTime(time);
        activityChannelMapper.insert(activityChannel);
    }

    @Override
    public ActivityChannel getActivityChannel(Long id) {
        ActivityChannel activityChannel = activityChannelMapper.findActivityChannelById(id);
        return activityChannel;
    }
}
